﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_ThongTinCauHoi : System.Web.UI.Page
{
    clsCauHoi frCH = new clsCauHoi();
    DataClassesDataContext db = new DataClassesDataContext();

    protected void Page_Load(object sender, EventArgs e)
    {
        btnThemCH.Attributes.Add("onclick", "return ktRadioButton();");
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["mach"] != "" && Request.QueryString["mach"] != null)
            {
                LoadCHSua();
            }
            
        }
    }
    public void LoadCHSua()
    {
        clsCauHoi chitietch=new clsCauHoi();
        chitietch=frCH.LayCHtheoMa(Request.QueryString["mach"]);

        txtNoidungCH.Text = chitietch.NoidungCH1;
        txtA.Text = chitietch.Da11;
        txtB.Text = chitietch.Da21;
        txtC.Text = chitietch.Da31;
        txtD.Text = chitietch.Da41;
       
    }
    public void Hamdong()
    {
        // txtMaCH.Enabled = false;
        txtNoidungCH.Enabled = false;
        txtA.Enabled = false;
        txtB.Enabled = false;
        txtC.Enabled = false;
        txtD.Enabled = false;
        // drplDAdung.Enabled = false;
        drplMucdo.Enabled = false;

    }

    public void Trangthaibd()
    {
        btnThemCH.Text = "Them CH";
        btnSua.Text = "Sua CH";
        btnHuy.Text = "Huy Thao Tac";
        btnThemCH.Enabled = true;
        btnXoaCH.Enabled = true;
        btnSua.Enabled = true;

        Hamdong();

    }

    protected void btnThemCH_Click(object sender, EventArgs e)
    {
        //if (btnThemCH.Text == "Them CH")
        //{
        //    //txtMaCH.Enabled = true;
        //    txtNoidungCH.Enabled = true;
        //    txtA.Enabled = true;
        //    txtA.Text = null;
        //    txtB.Enabled = true;
        //    txtB.Text = null;
        //    txtC.Enabled = true;
        //    txtC.Text = null;
        //    txtD.Enabled = true;
        //    txtD.Text = null;
        //    //drplDAdung.Enabled = true;
        //    //drplDAdung.Items.Clear();
        //    drplMucdo.Enabled = true;
        //    btnThemCH.Text = "Luu CH";
        //    //btnXoaCH.Enabled = false;
        //    //btnSua.Enabled = false;
        //}
        //else 
        //    if (btnThemCH.Text == "Luu CH")
        //{

            //string mach = txtMaCH.Text;
            string noidungch = txtNoidungCH.Text;
            string da1 = txtA.Text;
            string da2 = txtB.Text;
            string da3 = txtC.Text;
            string da4 = txtD.Text;
            int dokho = Convert.ToInt32(drlMonHoc.SelectedItem.Value);
            string machuong = drlChuongHoc.SelectedValue;

            List<RadioButton> check = new List<RadioButton>();
            check.Add(rdbA);
            check.Add(rdbB);
            check.Add(rdbC);
            check.Add(rdbD);
            List<string> da = new List<string>();
            da.Add(da1);
            da.Add(da2);
            da.Add(da3);
            da.Add(da4);
            string dapan = string.Empty;
            for (int i = 0; i < 4; i++)
            {
                if (check[i].Checked)
                {
                    dapan = da[i];
                    break;
                }

            }


            if (Request.QueryString["mach"] != "" && Request.QueryString["mach"] != null)
            {
                if (frCH.SuaCH(Request.QueryString["mach"], noidungch, da1, da2, da3, da4, dapan, dokho, machuong))
                {
                    //Response.Write("<script>alert('Sửa thành công') ; window.location.href='./frmAdTaoCH.aspx'</script>");
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Thông báo", "alert('Sửa thành công!');", true);
                    
                }
                else
                {
                    
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Thông báo", "alert('Câu hỏi này không được phép sửa đổi!');", true);
                   
                }
            }
            else
            {
                try
                {
                    frCH.ThemCHmoi(noidungch, da1, da2, da3, da4, dapan, dokho, machuong);
                    Response.Redirect("./frmAdTaoCH.aspx");
                }
                catch { 
                }
                
            }
            //GridView1.DataBind();
            //rfvMaCH.ControlToValidate = txtMaCH.ID;
            //rfvnoidung.ControlToValidate = txtNoidungCH.Text;
            // rfvDaA.ControlToValidate = txtA.Text;




            //btnThemCH.Text = "Them CH";
            //rdbA.Checked = false;
            //rdbB.Checked = false;
            //rdbC.Checked = false;
            //rdbD.Checked = false;
            //Trangthaibd();

            //


        //}
    }
    protected void btnXoaCH_Click(object sender, EventArgs e)
    {

    }
    protected void btnSua_Click(object sender, EventArgs e)
    {

    }
    protected void btnHuy_Click(object sender, EventArgs e)
    {

    }
    protected void txtA_TextChanged(object sender, EventArgs e)
    {
        string nda = txtA.Text;
        if (btnSua.Text == "Luu CH")
        {

            //drplDAdung.Items[0].Text = txtA.Text;
        }
        if (btnThemCH.Text == "Luu CH")
        {
            //ThemDAvaodrl(nda);
        }
        txtB.Focus();
    }

    protected void txtB_TextChanged(object sender, EventArgs e)
    {
        string nda = txtB.Text;
        if (btnSua.Text == "Luu CH")
        {

            //drplDAdung.Items[1].Text = txtB.Text;
        }
        if (btnThemCH.Text == "Luu CH")
        {
            //ThemDAvaodrl(nda);
        }
    }
    protected void txtC_TextChanged(object sender, EventArgs e)
    {
        string nda = txtC.Text;
        if (btnSua.Text == "Luu CH")
        {

            //drplDAdung.Items[2].Text = txtC.Text;
        }
        if (btnThemCH.Text == "Luu CH")
        {
            //ThemDAvaodrl(nda);
        }
    }
    protected void txtD_TextChanged(object sender, EventArgs e)
    {
        string nda = txtD.Text;
        if (btnSua.Text == "Luu CH")
        {

            //drplDAdung.Items[3].Text = txtD.Text;
        }
        if (btnThemCH.Text == "Luu CH")
        {
            // ThemDAvaodrl(nda);
        }
    }
}
